package com.cmse.persistence.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;

import com.cmse.persistence.dao.IArticalCommentDao;
import com.cmse.persistence.domain.ArticalComment;
import com.cmse.persistence.enmu.FetchOrderType;
import com.cmse.persistence.enmu.VerifyType;

/**
 * @author wulibin, exceptsky@gmail.com, 2012-3-15 下午1:05:05
 * 
 */
@SuppressWarnings("rawtypes")
public class ArticalCommentDao extends BaseDao<ArticalComment> implements IArticalCommentDao {
	private static Logger          logger           = Logger.getLogger(ArticalCommentDao.class);
	
	@Override
	public long getCountOfObj(long aid, VerifyType verifyType) {
		String hql = "select count(ac.id) from ArticalComment as ac where ac.artical.id = ? and"+getVerifySql("ac",verifyType);
		try {
			List rs = getHibernateTemplate().find(hql, aid);
			if(rs==null || rs.size()==0){
				return 0l;
			}
			long count = (Long)rs.listIterator().next();
			return count;
		} catch (Exception e) {
			logger.error(e);
			return 0l;
		}
	}

	@Override
	public long getCountOfUser(long uid, VerifyType verifyType) {
		String hql = "select count(ac.id) from ArticalComment as ac where ac.artical.id = ? and"+getVerifySql("ac",verifyType);
		try {
			List rs = getHibernateTemplate().find(hql, uid);
			if(rs==null || rs.size()==0){
				return 0l;
			}
			long count = (Long)rs.listIterator().next();
			return count;
		} catch (Exception e) {
			logger.error(e);
			return 0l;
		}
	}

	@Override
	public List<ArticalComment> getOfObj(long aid, int offset,int length, VerifyType verifyType, FetchOrderType orderType) {
		String hql = "from ArticalComment as ac where ac.artical.id = ? and";
		hql += " and"+getVerifySql("ac",verifyType);
		hql += getOrderSql("ac",orderType);
		return selectByPage(hql, new Long[]{new Long(aid)}, offset, length);
	}
}
